
package com.midian.action.mc.activity;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.midian.action.mc.common.AjaxAction;
import com.midian.common.annotation.FileModelDetails;
import com.midian.common.annotation.FunctionAnnotationOverride;
import com.midian.common.exception.BizException;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.DateUtils;
import com.midian.model.activity.Activity;
import com.midian.model.activity.ActivityType;
import com.midian.model.common.FileModel;
import com.midian.model.organization.Organization;
import com.midian.model.user.User;
import com.midian.service.activity.IActivityService;
import com.midian.service.activity.IActivityTypeService;
import com.midian.service.organization.IOrganizationService;
import com.midian.service.pic.IPicService;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.WKTReader;

/**
 * 活动Action
 * 
 * @author Mengfh
 *
 */
@Scope("prototype")
@Controller("activityAction")
@FunctionAnnotationOverride(isAction = true, auth = "活动管理")
public class ActivityAction extends AjaxAction<Activity> {
	/** 活动实体 */
	private Activity entity;
	/** 活动实体列表 */
	private List<Activity> rows;
	/** 活动业务 */
	private IActivityService activityService;
	/** 机构列 */
	private List<Organization> organizList;
	/** 机构业务 */
	private IOrganizationService organizationService;
	/** 活动类型列 */
	private List<ActivityType> activityTypeList;
	/** 活动类型业务 */
	private IActivityTypeService activityTypeService;
	/** 封面图片 */
	private FileModel coverPic;
	/** 图片业务 */
	private IPicService picService;

	@Resource(name = "activityTypeService")
	public void setActivityTypeService(IActivityTypeService activityTypeService) {
		this.activityTypeService = activityTypeService;
	}

	@Resource(name = "picService")
	public void setPicService(IPicService picService) {
		this.picService = picService;
	}

	@Resource(name = "organizationService")
	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public List<ActivityType> getActivityTypeList() {
		return activityTypeList;
	}

	public void setActivityTypeList(List<ActivityType> activityTypeList) {
		this.activityTypeList = activityTypeList;
	}

	@FileModelDetails(fieldName = "coverPic.id", isDelOldFile = false)
	public FileModel getCoverPic() {
		return coverPic;
	}

	public void setCoverPic(FileModel coverPic) {
		this.coverPic = coverPic;
	}

	public List<Organization> getOrganizList() {
		return organizList;
	}

	public void setOrganizList(List<Organization> organizList) {
		this.organizList = organizList;
	}

	public Activity getEntity() {
		return entity;
	}

	public void setEntity(Activity entity) {
		this.entity = entity;
	}

	@Resource(name = "activityService")
	public void setActivityService(IActivityService activityService) {
		this.service = activityService;
		this.activityService = activityService;
	}

	@Override
	protected Activity dytEntity() {
		if (this.entity == null) {
			this.entity = new Activity();
		}
		return getEntity();
	}

	@Override
	protected void dyEntity(Activity t) {
		setEntity(t);
	}

	@Override
	public void setRows(List<Activity> rows) {
		this.rows = rows;
	}

	@Override
	public List<Activity> getRows() {
		return rows;
	}

	@Override
	public String ajax() {
		try {
			organizList = this.organizationService.getOrginzations();
			activityTypeList = this.activityTypeService.getActivityTypes();
		} catch (BizException e) {
			e.printStackTrace();
		}
		return super.ajax();
	}

	@Override
	public String showInfo() {
		try {
			organizList = this.organizationService.getOrginzations();
			activityTypeList = this.activityTypeService.getActivityTypes();
		} catch (BizException e) {
			e.printStackTrace();
		}
		return super.showInfo();
	}

	@Override
	public String saveInfo() {
		try {
			
			if (this.entity == null) {
				this.entity = new Activity();
			}
			
			if(entity.getBeginTimeString()!=null){
				Date beginDate = DateUtils.formatDateStringToDate(entity.getBeginTimeString(), "yyyy-MM-dd HH:mm");
				entity.setBeginTime(beginDate);
			}
			//保存point类型地址参数
			if(entity.getActivityAddressLon()!=null && entity.getActivityAddressLat()!=null)
			{
				WKTReader fromText = new WKTReader();
				entity.setActivityAddressLocation((Point)fromText.read("POINT("+entity.getActivityAddressLon()+" "+entity.getActivityAddressLat()+")"));
			}
			
			//替换中文逗号为英文逗号
			if (entity.getAges()!=null) {
				String newAges = entity.getAges().replace("，", ",");
				entity.setAges(newAges);
			}
			
			String id = dytEntity().getId();
			if(StringUtils.isBlank(id)){
				//设置新建活动已经报名人数初始化为0
				this.entity.setJoinCount(0);
				service.add(dytEntity());
			}else{
				super.setId(id); //设置Action的id属性，flexigrid操作数据使用
				service.update(dytEntity());
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SAVEINFO;
	}

}
